package gold.digger;

import gold.vo.ListNode;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC19 {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode front = head;
        ListNode back = head;
        // make front step n first
        while (null != front && n-- > -1) {
            front = front.next;
        }

        if (n == 0) {
            head = head.next;
            return head;
        }

        while (null != front) {
            back = back.next;
            front = front.next;
        }

        //locate position, start to delete element
        back.next = back.next.next;

        return head;
    }

    public void testMethod() {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
//        tail.next.next = new ListNode(3);
//        tail.next.next.next = new ListNode(4);
//        tail.next.next.next.next = new ListNode(5);
        removeNthFromEnd(head, 2);
    }

    public static void main(String[] args) {
        LC19 an = new LC19();
        an.testMethod();
    }
}
